import cmath

class Decimal():
    def __init__(self,remain):
        self.remain = remain
    #判断是否为nan or inf，是返回true
    def isnan_orinf(self,num):
        if cmath.isnan(num) != True and cmath.isinf(num) != True:
            return False
        else: return True
    #为某列数据保留小数点
    def reserved_decimal(self,data, change_column_list,):
        decimal_num = self.remain
        for column in change_column_list:
            data[column] = data[column].round(decimal_num)
    # 转为百分比
    def to_percentage(self,data, change_column_list):
        decimal_num = self.remain
        reserved_decimal = '.' + str(decimal_num) + '%'

        def change(x):
            if cmath.isnan(x) != True and cmath.isinf(x) != True:
                x = format(x, reserved_decimal)
            return x

        for column in change_column_list:
            data[column] = data[column].apply(change)

        return data
    def to_percent(self,num,tostr=False):
        reserved_decimal = '.' + str(self.remain) + '%'
        if self.isnan_orinf(num) != True: #判断非空
                num = format(num, reserved_decimal) #保留小数点
        else:
            if tostr != False:
                if cmath.isnan(num) == True:
                    num = 'null'
                elif cmath.isinf(num) == True:
                    num = 'inf'
        return num
    def list_to_percentage(self,change_list,tostr=False):
        return [self.to_percent(i,tostr=tostr) for i in change_list]
    # 将数值转变为百分比
    def to_percent_str(self, num, to_percent, to_pp, pp_mul100):
        decimal_num = self.remain
        if cmath.isnan(num) == True:
            return None
        elif cmath.isinf(num) == True:
            return None
        if (to_percent != True) & (to_pp != True):
            return str(num)
        elif (to_percent == True) & (to_pp == True):
            print('to_percent 与 to_pp 中只能有一个为 True')
            return str(num)
        elif to_percent == True:
            reserved_decimal = '.' + str(decimal_num) + '%'
            return format(num, reserved_decimal)
        elif to_pp == True:
            if pp_mul100 == True:
                reserved_decimal = '.' + str(decimal_num + 2)
                num = num * 100
                return format(num, reserved_decimal) + 'pp'
            else:
                reserved_decimal = '.' + str(decimal_num)
                return format(num, reserved_decimal) + 'pp'
    # 将百分比转换为float
    def percentage_to_float(self,data, change_column_list):
        for column in change_column_list:
            def replace(x):
                if type(x) == str:
                    x = x.replace('%', '')
                return x
            data[column] = data[column].apply(replace)  # 去去掉%
            data[column] = data[column].astype('float')
            data[column] = data[column] / 100
    def round(self,num,tostr=False,remain = None):
        #保留为None就用默认值
        if remain == None:
            remain = self.remain
        if isinstance(num, str) != True:
            if tostr==False:
                if (cmath.isnan(num) == True)or(cmath.isinf(num) == True):
                    pass
                if int(num) == num:
                    num = int(num)
                else:
                    num = round(num, remain)
            else:
                if cmath.isnan(num) == True:
                    num = 'null'
                elif cmath.isinf(num) == True:
                    num = 'inf'
                else:
                    if int(num) == num:
                        num = str(int(num))
                    else:
                        num = str(round(num,remain))
        return num

